package com.zoweunion.mechanic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.dao.NeedLeaseDao;
import com.zoweunion.mechanic.entity.NeedLeaseEntity;
import com.zoweunion.mechanic.entity.TSSsVehicleBrandEntity;
import com.zoweunion.mechanic.util.Orgin;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.zoweunion.mechanic.dao.LeaseDao;
import com.zoweunion.mechanic.entity.LeaseEntity;
import com.zoweunion.mechanic.service.LeaseService;

import static java.util.stream.Collectors.toList;


@Service("leaseService")
public class LeaseServiceImpl extends ServiceImpl<LeaseDao, LeaseEntity> implements LeaseService {

    @Autowired
    private LeaseDao leaseDao;
    @Autowired
    private NeedLeaseDao needLeaseDao;


    @Override
    public Map<String,Object> queryPage(Map<String, Object> map)  {
        long currentPage = Orgin.getLongValue(map, "currentPage");
        long pageSize = Orgin.getLongValue(map, "pageSize");
        List<LeaseEntity> mapList=new ArrayList<>();
        if (leaseDao.queryPage(map).size()!=0){
            mapList.addAll(leaseDao.queryPage(map));
        }
        //查询已绑定carid
        if (leaseDao.selectCarId(map).size()!=0){
            mapList.addAll(leaseDao.selectCarId(map));
        }
        //查询未绑定carid
        if (leaseDao.selectNotCarIdByUserId(map).size()!=0){
            mapList.addAll(leaseDao.selectNotCarIdByUserId(map));
        }

        Map<String, Object> map1 = new HashMap<>();
        map1.put("count",mapList.size());
        map1.put("data",mapList.stream().skip((currentPage - 1) * pageSize).limit(pageSize)
                .collect(toList()));
        return map1;
    }

    @Override
    public List<Object> mapList(Map<String, Object> map) {
        int finalCode = Orgin.getIntValue(map, "finalCode");
        String typeName = Orgin.getString(map, "typeName");
        if (!typeName.isEmpty()){
            List<String> strsToList1= Arrays.asList(typeName.split(","));
            typeName = "'" + StringUtils.join(strsToList1, "','") + "'";
            map.put("typeName",typeName);
        }
        List<Object> mapList=new ArrayList<>();
        if (leaseDao.queryPage(map).size()!=0){
            mapList.addAll(leaseDao.queryPage(map));
        }
        //查询已绑定carid
        if (leaseDao.selectCarIdN(map).size()!=0){
            mapList.addAll(leaseDao.selectCarIdN(map));
        }
        //查询未绑定carid
        if (leaseDao.selectNotCarId(map).size()!=0){
            mapList.addAll(leaseDao.selectNotCarId(map));
        }
        //查询求租
        if (leaseDao.selectNeedLease(map).size()!=0&&typeName.isEmpty()){
            mapList.addAll(leaseDao.selectNeedLease(map));
        }
       if (finalCode!=0){
           mapList = mapList.stream()
                   .filter(o -> Orgin.getIntValue(new org.apache.commons.beanutils.BeanMap(o)
                           , "provinceCode") == finalCode ||
                           Orgin.getIntValue(new org.apache.commons.beanutils.BeanMap(o)
                                   , "cityCode") == finalCode ||
                           Orgin.getIntValue(new org.apache.commons.beanutils.BeanMap(o)
                                   , "districtCode") == finalCode)
                   .collect(Collectors.toList());
       }
        return mapList;
    }

    @Override
    public List<String> mapSearchForList(Map<String, Object> map) {
        List<LeaseEntity> mapList=new ArrayList<>();
        if (leaseDao.queryPage(map).size()!=0){
            mapList.addAll(leaseDao.queryPage(map));
        }
        //查询已绑定carid
        if (leaseDao.selectCarIdN(map).size()!=0){
            mapList.addAll(leaseDao.selectCarIdN(map));
        }
        //查询未绑定carid
        if (leaseDao.selectNotCarId(map).size()!=0){
            mapList.addAll(leaseDao.selectNotCarId(map));
        }
        List<String> collect = mapList.stream().map(LeaseEntity::getTypeName).distinct()
                .collect(toList());
        return collect.stream().filter(x -> x!=null).collect(Collectors.toList());
    }

    @Override//
    public Map<String, Object> ownLeaseList(Map<String, Object> map) {
        long currentPage = Orgin.getLongValue(map, "currentPage");
        long pageSize = Orgin.getLongValue(map, "pageSize");
        int type = Orgin.getIntValue(map, "type");
        int onStatus = Orgin.getIntValue(map, "onStatus");
        List<Object> mapList=new ArrayList<>();
        if (type==1){
            if (leaseDao.queryPageById(map).size()!=0){
                mapList.addAll(leaseDao.queryPageById(map));
            }
            //查询已绑定carid 
            if (leaseDao.selectCarId(map).size()!=0&&onStatus!=1){
                mapList.addAll(leaseDao.selectCarId(map));
            }
        }else if (type==2){
            mapList.addAll(needLeaseDao.queryPage(map));
        }

        Map<String, Object> map1 = new HashMap<>();
        map1.put("count",mapList.size());
        map1.put("data",mapList.stream().skip((currentPage - 1) * pageSize).limit(pageSize)
                .collect(toList()));
        return map1;
    }

    @Override
    public LeaseEntity getInfoId(long id) {
        return leaseDao.getInfoId(id);
    }

}